﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// Bring extension methods into scope
using TheClerk.Forms.Templates.raw.Envelope;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.X509;

namespace TheClerk.Forms.Templates.raw.Envelope
{
    /// <summary>
    /// Contains methods to interact with the underlying implementation
    /// </summary>
    static class PlatformExtensionMethods
    {
        internal static byte[] AsByteArray(this System.IO.Stream input)
        {
            var output = new byte[input.Length];
            input.Position = 0;
            input.Read(output, 0, checked((int)input.Length));
            return output;
        }


        internal static byte[] GetEncoded(this object input)
        {
            
            throw new NotImplementedException();
            //// for output
            //System.IO.MemoryStream stream = new System.IO.MemoryStream();

            //var outputArray = new byte[stream.Length];

            //// Simplest way: if it implements its own converter, use it
            //if (input is IAsn1Convertible)
            //{
            //    var input2 = input as IAsn1Convertible;
            //    Asn1Object outobj = input2.ToAsn1Object();
            //    return outobj.GetDerEncoded();
            //}
            //else if (input is StrongBox)
            //{
            //    var collection = (input as StrongBox).Collection;
            //    var s = new DerSequenceGenerator(stream);
            //    // We need to construct the extension
            //    // We can't until we have the DER sequence

            //    // for each thing in collection, we need to create 3 things:
            //    // algorithmOID,
            //    // optional destination byte[],
            //    // signature bitstring
            //    foreach (StrongBoxItem i in collection) {
            //        //s.AddObject(new Asn1OctetString(i.AsDer()));
            //    }
                
                
            //}
            //else if


            //   (input is ICollection<StrongBoxItem>)
            //{
            //    var input2 = input as ICollection;
            //    var s = new DerSequenceGenerator(stream);
            //    foreach (object i in input2)
            //        ;

            //}
            //if (stream.Length > System.Int32.MaxValue)
            //    throw new OverflowException("Encoding is >2GB");
            //stream.Read(outputArray, 0, (int)stream.Length);
            //return outputArray;
        }

        internal static void GenerateKeyPairs()
        {
            throw new NotImplementedException();
        }
    }


}
